package com.coocaa.app.lib.cleaner.scanner.config.impl;

import android.text.TextUtils;
import android.util.Log;

import com.coocaa.app.lib.cleaner.scanner.config.ConfigDataFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lu on 17-4-17.
 */

public class ConfigDataImpl implements ConfigDataFactory.ConfigData {

    Map<String, List<String>> packageKeep = new HashMap<String, List<String>>();
    Map<String, List<String>> packageDel = new HashMap<String, List<String>>();

    List<String> fileKeep = new ArrayList<String>();
    List<String> fileDel = new ArrayList<String>();

    List<String> ignorePackages = new ArrayList<String>();

    List<String> cacheKeep = new ArrayList<String>();

    public ConfigDataImpl() {
    }

    @Override
    public ConfigDataFactory.ConfigData appendAll(ConfigDataFactory.ConfigData data) {
        ConfigDataImpl i = (ConfigDataImpl) data;
        synchronized (packageKeep) {
//            packageKeep.putAll(i.packageKeep);
            if(i.packageKeep != null && i.packageKeep.size() > 0) {
                for(String key:i.packageKeep.keySet()) {
                    if (!packageKeep.containsKey(key))
                        packageKeep.put(key,i.packageKeep.get(key));
                    else{
                        List<String> list = packageKeep.get(key);
                        list.addAll(i.packageKeep.get(key));
                    }
                }
            }
        }
        synchronized (packageDel) {
//            packageDel.putAll(i.packageDel);
            if(i.packageDel != null && i.packageDel.size() > 0) {
                for(String key:i.packageDel.keySet()) {
                    if (!packageDel.containsKey(key))
                        packageDel.put(key,i.packageDel.get(key));
                    else{
                        List<String> list = packageDel.get(key);
                        list.addAll(i.packageDel.get(key));
                    }
                }
            }
        }
        synchronized (fileKeep) {
            fileKeep.addAll(i.fileKeep);
        }
        synchronized (fileDel) {
            fileDel.addAll(i.fileDel);
        }
        synchronized (ignorePackages) {
            ignorePackages.addAll(i.ignorePackages);
        }
        synchronized (cacheKeep) {
            cacheKeep.addAll(i.cacheKeep);
        }
        return this;
    }

    @Override
    public ConfigDataFactory.ConfigData appendPackageKeep(String packageName, String file) {
        synchronized (packageKeep) {
            List<String> l = packageKeep.get(packageName);
            if (l == null) {
                l = new ArrayList<String>();
                packageKeep.put(packageName, l);
            }
            synchronized (l) {
                if (!l.contains(file))
                    l.add(file);
            }
        }
        return this;
    }

    @Override
    public ConfigDataFactory.ConfigData appendPackageDel(String packageName, String file) {
        synchronized (packageDel) {
            List<String> l = packageDel.get(packageName);
            if (l == null) {
                l = new ArrayList<String>();
                packageDel.put(packageName, l);
            }
            synchronized (l) {
                if (!l.contains(file))
                    l.add(file);
            }
        }
        return this;
    }

    @Override
    public ConfigDataFactory.ConfigData appendFileKeep(String file) {
        synchronized (fileKeep) {
            if (!fileKeep.contains(file))
                fileKeep.add(file);
        }
        return this;
    }

    @Override
    public ConfigDataFactory.ConfigData appendFileDel(String file) {
        synchronized (fileDel) {
            if (!fileDel.contains(file))
                fileDel.add(file);
        }
        return this;
    }

    @Override
    public boolean containsPackageKeep(String packageName, String file) {
        synchronized (packageKeep) {
            List<String> l = packageKeep.get(packageName);
            if (l != null)
                return l.contains(file);
        }
        return false;
    }

    @Override
    public List<String> getFileKeepFiles() {
        synchronized (fileKeep) {
            return new ArrayList<String>(fileKeep);
        }
    }

    @Override
    public List<String> getPackageKeepFiles(String packageName) {
        synchronized (packageKeep) {
            List<String> keeps = packageKeep.get(packageName);
            if (keeps != null) {
                synchronized (keeps) {
                    for(String str:keeps)
                    Log.d("pscan","getPackageKeepFiles str "+str);
                    return new ArrayList<String>(keeps);
                }
            }
        }
        return new ArrayList<String>();
    }

    @Override
    public List<String> getFileDelFiles() {
        synchronized (fileDel) {
            return new ArrayList<String>(fileDel);
        }
    }

    @Override
    public List<String> getPackageDelFiles(String packageName) {
        synchronized (packageDel) {
            List<String> dels = packageDel.get(packageName);
            if (dels != null) {
                synchronized (dels) {
                    return new ArrayList<String>(dels);
                }
            }
        }
        return new ArrayList<String>();
    }

    @Override
    public boolean containsFileKeep(String file) {
        synchronized (fileKeep) {
            return fileKeep.contains(file);
        }
    }

    @Override
    public ConfigDataFactory.ConfigData appendIgnorePackage(String packageName) {
        synchronized (ignorePackages) {
            if (!ignorePackages.contains(packageName))
                ignorePackages.add(packageName);
        }
        return this;
    }

    @Override
    public boolean containsIgnorePackage(String packageName) {
        synchronized (ignorePackages) {
            return ignorePackages.contains(packageName);
        }
    }

    @Override
    public boolean isFullScan(String packageName) {
        boolean keep, del;
        synchronized (packageKeep) {
            keep = packageKeep.containsKey(packageName);
        }

        synchronized (packageDel) {
            del = packageDel.containsKey(packageName);
        }
        return !keep && !del;
    }

    @Override
    public boolean containsPackageDel(String packageName, String file) {
        synchronized (packageDel) {
            List<String> dels = packageDel.get(packageName);
            if (dels != null) {
                synchronized (dels) {
                    return dels.contains(file);
                }
            }
        }
        return false;
    }

    @Override
    public boolean containsFileDel(String file) {
        synchronized (fileDel) {
            return fileDel.contains(file);
        }
    }

    @Override
    public void appendCacheKeep(String packageName) {
        synchronized (cacheKeep) {
            if (!TextUtils.isEmpty(packageName))
                cacheKeep.add(packageName);
        }
    }

    @Override
    public boolean containsCacheKeep(String packageName) {
        synchronized (cacheKeep) {
            if (!TextUtils.isEmpty(packageName))
                return cacheKeep.contains(packageName);
        }
        return false;
    }
}
